package com.yunkeji.api.invited.controller;

import com.yunkeji.api.invited.bo.BindBo;
import com.yunkeji.api.invited.service.InvitedService;
import com.yunkeji.api.invited.vo.InvitedCount;
import com.yunkeji.api.invited.vo.InvitedVo;
import com.yunkeji.api.invited.vo.QueryDownLoad;
import com.yunkeji.common.annotation.RepeatSubmit;
import com.yunkeji.common.core.controller.BaseController;
import com.yunkeji.common.core.domain.PageQuery;
import com.yunkeji.common.core.domain.R;
import com.yunkeji.common.core.page.TableDataInfo;
import com.yunkeji.dao.domain.InviteReward;
import com.yunkeji.dao.mapper.SysVersionMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.concurrent.TimeUnit;

/**
 * 邀请相关
 *
 * @author 老张
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/v1/invited")
public class InvitedController extends BaseController {
    private final InvitedService invitedService;
    private final SysVersionMapper sysVersionMapper;

    /**
     * 获取下载链接
     *
     * @author 老张
     */
    @PostMapping("/queryDownLoad")
    @RepeatSubmit(
            interval = 1,
            timeUnit = TimeUnit.MINUTES,
            message = "获取下载链接过快,请稍后再试",
            profile = "prod")
    public R<QueryDownLoad> queryDownLoad(HttpServletRequest httpServletRequest) {
        return R.ok(invitedService.bindPid(httpServletRequest));
    }

    /**
     * 邀请好友分页
     *
     * @author 老张
     */
    @PostMapping("/pageInvited")
    public TableDataInfo<InvitedVo> pageInvited(@RequestBody PageQuery pageQuery) {
        TableDataInfo<InvitedVo> datas = invitedService.pageInvited(pageQuery);
        return datas;
    }

    /**
     * 邀请收益统计
     *
     * @author 老张
     */
    @PostMapping("/invitedCount")
    public R<InvitedCount> invitedCount() {
        return R.ok(invitedService.invitedCount());
    }

    /**
     * 绑定邀请好友
     *
     * @author 老张
     */
    @PostMapping("/toBind")
    public R<InvitedCount> toBind(@RequestBody @Valid BindBo bindBo) {
        invitedService.toBind(bindBo);
        return R.ok();
    }
}
